<?php
namespace Admin\Controller;
use Admin\Model\OrderModel;
use \Common\Model\CommonModel;
use Think\Controller;

class OrderController extends BaseController{

    public function index(){

        //主页
            $orderModel=M('order');
            $condition['name'] = array('like',"%".$_GET['name']."%");
            unset($_SESSION['like']['order']);
            $_SESSION['like']['order']=$_GET['name'];
            if($_GET['start']&&$_GET['end']){
                $condition['create']= array('between',array(strtotime($_GET['start']),strtotime($_GET['end'])));
            }

            $count = $orderModel->where($condition)->count();
            $page= new \Think\Page($count,5);
            $page->setConfig('next','下一页');
            $page->setConfig('prev','上一页');
            //显示还有多少条
            $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $page->show();
            $list=$orderModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
            $list=CommonModel::order($list);
            $this->assign('page',$show);
            $this->assign('list',$list);
            $this->display();

    }
    //修改页面
    public function update(){
        $orderModel=D('order');
        //$orderModel=new PriceModel();
        $da['id']=$_GET['id'];
        //找到原数据
        $list=$orderModel->finded($da);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        $orderModel=D('order');
        $data['mobile']=$_POST['mobile'];
        $data['name']=$_POST['name'];
        $id=$_POST['id'];
        $where='id='.$id;
       // echo $id;exit;
        $list=$orderModel->finded($where);
        /* echo(json_encode($list));
         exit;*/
        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
       // echo json_encode($data);
       // exit;
        $res=$orderModel->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }
    //删除
    public function del()
    {
        $orderModel = D('order');
        $id = $_GET['id'];
        $res = $orderModel->deleted('id=' . $id);
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }

    }
}